
#include<iostream>
#include<vector>
#include<fstream>
#include<algorithm>
using namespace std;
vector<int> p;
void build(int n) {
    for (p.push_back(2), p.push_back(3); *p.rbegin() < n;) {
        for (int j = *p.rbegin(), k = j + 2, i; k <= j*j; k += 2) {
            for (i = 1; i < p.size(); i++)
                if (k%p[i] == 0)
                    break;
            if (i == p.size())
                p.push_back(k);
            else
                j=k;
        }
    }
}
int main() {
    build(1000000+5);
    ifstream cin("24.txt");
    for (int m; cin >> m;)
        for (int n,left,right; m-- && cin >> n;) {
            left=n,right=n+1;
             while(!binary_search(p.begin(), p.end(), left))--left;
             while(!binary_search(p.begin(), p.end(), right))++right;
            if(n-left<right-n)
                cout<<left<<' '<<n-left<<endl;
            else
                cout<<right<<' '<<right-n<<endl;
        }
    return 0;
}
